package com.wx.controller;

import com.wx.entity.SmbmsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Controller
public class TestRedisController {

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/set")
    public void set(@RequestBody SmbmsUser smbmsUser){
        redisTemplate.opsForValue().set("smbmsUser",smbmsUser);
    }
    @GetMapping("/get/{key}")
    public SmbmsUser get(@PathVariable("key") String key){
        return (SmbmsUser) redisTemplate.opsForValue().get(key);
    }
    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("key") String key){
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }


    @GetMapping("/string")
    public String stringTest(){
        redisTemplate.opsForValue().set("str","Hello World");
        String str = (String) redisTemplate.opsForValue().get("str");
        return str;
    }
    @GetMapping("/list")
    public List<String> listTest(){
        ListOperations<String,String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("list","Hello");
        listOperations.leftPush("list","World");
        listOperations.leftPush("list","Java");
        List<String> list = listOperations.range("list",0,2);
        return list;
    }
    @GetMapping("/set1")
    public Set<String> setTest(){
        SetOperations<String,String> setOperations = redisTemplate.opsForSet();
        setOperations.add("set","Hello");
        setOperations.add("set","Hello");
        setOperations.add("set","World");
        setOperations.add("set","World");
        setOperations.add("set","Java");
        setOperations.add("set","Java");
        Set<String> set = setOperations.members("set");
        return set;
    }
//    @GetMapping("/map")
//    public Set<String> setTest(){
////        HashOperations<String,String,String> hashOperations =
////                redisTemplate.opsForHash();
////        hashOperations.put(hashMap1,key1,value1);
////        hashOperations.put(hashMap2,key2,value2);
////        hashOperations.put(hashMap3,key3,value3)
////        Set<String> set = setOperations.members("set");
//        return null;
//    }


}
